<?php
/**
 * Created by PhpStorm.
 * User: Simpson
 * Date: 2016/8/30
 * Time: 22:15
 */

namespace App\Service;

use App\Events\OrderRepairSuccessEvent;
use App\Model\Admin;
use App\Model\Role;
use App\Model\User;
use App\Model\Repair;
use App\Model\Schedule;
use App\Model\Feedback;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use phpDocumentor\Reflection\Types\Array_;
use UserService;
use WechatService as Wechat;
use App\Repository\UserRepository;
use App\Repository\RepairRepository;
use App\Repository\ScheduleRepository;
class RepairService
{
    const REPAIR_SOURCE = 'repair';
    const REPAIR_COUNT = 30;

    protected $user;

    protected $order;

    protected $schedule;

    public function __construct(UserRepository $user, RepairRepository $order, ScheduleRepository $schedule)
    {
        $this->user = $user;

        $this->order = $order;

        $this->schedule = $schedule;
    }

    /**
     * 返回预约页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getView()
    {
        $user = \Auth::guard('wechat')->user();
        
        $repairs = $this->order->getOrderByOpenId($user->open_id);

        $schedules = $this->schedule->getSchedule();

        return view('app.repair.index', [
            'user' => $user,
            'repairs' => $repairs,
            'schedules' => $schedules,
        ]);
        
        
    }
    /**
     * 创建维护预约，获取并修改User信息
     * @param Request $request
     * @return bool
     */
    public function addRepair($platform, $description, $orderTime, $schedule, $cellPhone, $shortPhone, $address)
    {

        $user = \Auth::guard('wechat')->user();

        $result = false;

        $repair = $this->order->addOrder($user->open_id, $platform, $description, $orderTime, $schedule);

        if($repair)
            $result = $this->user->updateUserFromRepair($user->open_id, $platform, $cellPhone, $shortPhone, $address);

        return $result;

    }

    public function cancelOrder($id)
    {

        $user = \Auth::guard('wechat')->user();

        $order = $this->order->getOrder($id);

        if($order->open_id == $user->open_id && $order->status == 0){
            $order->status = -1;
            $order->save();

            return true;
        }

        return false;
    }

    
    /**
     * 获取APP用户维护权限信息
     * @param Admin $admin
     * @return bool
     */
    public function checkMinister(Admin $admin)
    {
        $role = $admin->roles()->where('name', 'repair_minister')->first();
        
        return $role ? true : false;
    }

    /**
     * 获取未维护预约信息
     * @return array
     */
    public function getRepair()
    {
        $where = [
            'status' => 0,
            'operator_id' => null,
        ];
        $repairs = $this->order->getRepairApi($where);

        return $repairs;
    }

    /**
     * 接维护任务
     * @param Admin $admin
     * @param $id
     * @return mixed
     */
    public function assignRepair(Admin $admin, $id)
    {
        
        $repair = Repair::where(['status' => 0, 'operator_id' => null])->find($id);

        if(! $repair)
            return false;

        $repair->status = 1;
        $repair->operator_id = $admin->id;
        $result = $repair->save();
        
        if($result){
            $user = $admin->user;
            
            event(new OrderRepairSuccessEvent($repair, $user));

        }

        return $result;
    }

    /**
     * 取消维护任务
     * @param Admin $admin
     * @param $id
     * @return bool
     */
    public function cancelRepair(Admin $admin, $id)
    {

        $repair = $this->order->getOrder($id);

        if(!$repair && $repair->operator_id != $admin->id)
            return false;



        $repair->status = 0;
        $repair->operator_id = null;
        $result = $repair->save();

        return $result;
    }

    /**
     * 查看已接的任务
     * @param Request $request
     * @return array
     */
    public function repairTask(Admin $admin)
    {
        $where = [
            'status' => 1,
            'operator_id' => $admin->id,
        ];

        $repairs = $this->order->getRepairApi($where);

        return $repairs;
    }

    /**
     * 完成维护任务
     * @param Admin $admin
     * @param $id
     * @return bool
     */
    public function updateRepair(Admin $admin, $id)
    {

        $repairTask = $this->order->getOrder($id);

        if(!$repairTask && $repairTask->operator_id != $admin->id && $repairTask->status == 1)
            return false;

        $repairTask->status = 2;
        $result = $repairTask->save();

        return $result;
    }

    /**
     * 部长分配干事维护任务
     * @param Admin $admin
     * @param $repair_id
     * @param $officer_id
     * @return bool|mixed
     */
    public function setRepair(Admin $admin, $officerId, $repairId)
    {

        $result = $this->checkMinister($admin);

        if(! $result) {
            return $result;
        }

        $repair = $this->order->getOrder($repairId);

        if(!$repair && $repair->operator_id != null && $repair->status != 0)
            return false;

        $repair->status = 1;
        $repair->operator_id = $officerId;
        $result = $repair->save();


        return $result;
    }

    /**
     * 获取已完成任务列表
     *
     * @param Admin $admin
     * @return mixed
     */
    public function getFinishOrder(Admin $admin)
    {
        $where = [
            'status' => 2,
            'operator_id' => $admin->id,
        ];

        $repairs = $this->order->getRepairApi($where);

        return $repairs;
    }

    /**
     * 获取所有已接任务
     *
     * @param $orderTime
     * @return mixed
     */
    public function getTask()
    {
        $where = [
            'status' => 1,
        ];

        $repairs = $this->order->getTask($where);

        return $repairs;
    }


}